<?php

namespace Home\Controller;
use Think\Controller; 
class GoodsController extends Controller {
	
	public function __construct() {
		parent::__construct();
		$ti = M('ti') -> where("user_id = '".$_SESSION['user']['id']."'") -> find();
		
		if(empty($ti) || empty($_SESSION['user']['id']))
		{
			$this->redirect('Home/Login/login');
		}
    }
	
	public function index(){
		$visitor_id = $_SESSION['user']['id'];
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		//$shop_id=empty($_REQUEST['shop_id'])?'':trim($_REQUEST['shop_id']);
		$title=empty($_REQUEST['title'])?'':trim($_REQUEST['title']);
		$code=empty($_REQUEST['code'])?'':trim($_REQUEST['code']);
		$short_name=empty($_REQUEST['short_name'])?'':trim($_REQUEST['short_name']);
		$name=empty($_REQUEST['name'])?'id':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		
		
		$goods =D('goods'); 
		//$result=$goods->goods_list($pageNo,$pageSize,$name,$asc,$title,$code,$short_name,$shop_id,$visitor_id);
		$result=$goods->goods_list($pageNo,$pageSize,$name,$asc,$title,$code,$short_name,$visitor_id);
		//$shop_list=M('shop')->where($where)->field('id,shop_name')->select();
		
		$this->assign("goodsinfo",$result['goodsinfo']);
		
		//$this->assign("shop_list",$shop_list);
		$this->assign("count",$result['count']);
		$this->assign("data",$result['data']);
		$this->display();
    }
	
	/* 添加商品 */
	function goods_info()
	{  
		$data['title']	    =	empty($_POST['title'])?'':trim($_POST['title']);
		$data['short_name']	=	empty($_POST['short_name'])?'':trim($_POST['short_name']);
		$data['code']	    =	empty($_POST['code'])?'':trim($_POST['code']);
		$data['shop_id']	=	empty($_POST['shop_id'])?'':trim($_POST['shop_id']);
		$data['price']   	=	empty($_POST['price'])?'':trim($_POST['price']);
		$data['cost']	    =	empty($_POST['cost'])?'':trim($_POST['cost']);
		$data['stock']	    =	empty($_POST['stock'])?'':trim($_POST['stock']);
		$data['warn_stock']	=	empty($_POST['warn_stock'])?'':trim($_POST['warn_stock']);
		$data['long']	    =	empty($_POST['long'])?'':trim($_POST['long']);
		$data['width']	    =	empty($_POST['width'])?'':trim($_POST['width']);
		$data['height']   	=	empty($_POST['height'])?'':trim($_POST['height']);
		$data['visitor_id']	= 	$_SESSION['user']['id'];
		
		$id=empty($_POST['id'])?0:$_POST['id'];
		$visitor_id = $_SESSION['user']['id'];
		$goods=M('goods');
		if(empty($id))
		{
			$res=$goods->add($data);
			header("Location:index");
		}
		else
		{
			$res=$goods->where("id='$id' and visitor_id = '$visitor_id'")->save($data);

			header("Location:index");
		}
	}
	
	/* 宝贝 双击修改*/
	public function goods_edit()
	{
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		$name=empty($_REQUEST['name'])?'':$_REQUEST['name'];
		$value=empty($_REQUEST['value'])?'':$_REQUEST['value'];
		$visitor_id = $_SESSION['user']['id'];
		$data[$name]=$value;
		$data['modify_time']=date('Y-m-d H:i:s');
		$where = "id = $id";
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
		}
		$set=M('goods');
		$res=$set->where($where)->save($data); 
		if($res!==false)
		{
			$msg='success';
		}
		else 
		{
			$msg='fail';
		}
		
		echo $msg;
	}
	
	// 删除商品
	public function goods_delete(){
		$visitor_id = $_SESSION['user']['id']; 
		$id=empty($_REQUEST['id'])?0:$_REQUEST['id'];
		$where = "id = $id";
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
		}
		$goods=M('goods');
		$res=$goods->where($where)->delete();
		
		header("Location:".$_SERVER['HTTP_REFERER']);
    }
	
	
	
	/*  宝贝设置 */
	public function set_goods(){
		
	    $pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$title=empty($_REQUEST['title'])?'':trim($_REQUEST['title']);
		$name=empty($_REQUEST['name'])?'id':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		
		$goods=D('goods');
		$res =  $goods->goodsset_list($pageNo,$pageSize,$name,$asc,$title);	

		$this -> assign('list',$res['goodsset_list']);
		$this -> assign('count',$res['count']);
		$this -> display();
	}
	
	/* 宝贝设置添加 */
	function set_goods_add()
	{
		$data['title']		 =	empty($_POST['title'])?'':trim($_POST['title']);
		$data['one']		 =	empty($_POST['one'])?'':trim($_POST['one']);
		$data['two']		 =	empty($_POST['two'])?'':trim($_POST['two']);
		$data['three']		 =	empty($_POST['three'])?'':trim($_POST['three']);
		$data['four']		 =	empty($_POST['four'])?'':trim($_POST['four']);
		$data['five']		 =	empty($_POST['five'])?'':trim($_POST['five']);
		$data['six']		 =	empty($_POST['six'])?'':trim($_POST['six']);
		$data['seven']		 =	empty($_POST['seven'])?'':trim($_POST['seven']);
		$data['eight']		 =	empty($_POST['eight'])?'':trim($_POST['eight']);
		$data['visitor_id']	 = 	$_SESSION['user']['id'];
		$id=empty($_POST['id'])?0:$_POST['id'];
		$visitor_id = $_SESSION['user']['id'];  
		$set=M('goods_set');
		
		$where['title']=$data['title'];
		$where['visitor_id']=$data['visitor_id'];
		$setinfo=$set->where($where)->find();
		if($setinfo)
		{
			$this->error('宝贝已经添加，不能重复添加');
		}
		else
		{
			if(empty($id))
			{
				$res=$set->add($data);
				
				header("Location:".$_SERVER['HTTP_REFERER']);
			}
			else
			{
				$res=$set->where("id='$id' and visitor_id = '$visitor_id'")->save($data);
				
				header("Location:".$_SERVER['HTTP_REFERER']);
			}
		}
		
	}
	 
	/* 宝贝设置 双击修改*/
	public function set_goods_edit()
	{	
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		$name=empty($_REQUEST['name'])?'':$_REQUEST['name'];
		$value=empty($_REQUEST['value'])?'':$_REQUEST['value'];
		
		$data[$name]=$value;
		$visitor_id = $_SESSION['user']['id'];
		$where = "id='$id'";
		$wheret= "title='".$value."' and id!=".$id;
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
			$wheret.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
			$wheret .= " and visitor_id = '$visitor_id'";
		}
		$set=M('goods_set');
		
		if($name=='title')
		{
			
			$setinfo=$set->where($wheret)->find();
			if($setinfo)
			{
				$msg='宝贝已经添加过，不能重复添加';
				echo $msg; exit;
			}
		}
		
		$res=$set->where($where)->save($data);
		if($res!==false)
		{
			$msg='success';
		}
		else
		{
			$msg='fail';
		}
		
		echo $msg;
	}
	
	/* 宝贝设置 删除 */
	public function set_goods_delete(){
		$visitor_id = $_SESSION['user']['id'];  
		$id=empty($_REQUEST['id'])?0:$_REQUEST['id'];
		$where="id='$id'";
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
		}
		$huodong=M('goods_set');
		$res=$huodong->where($where)->delete();
		
		header("Location:".$_SERVER['HTTP_REFERER']);
    }
	
	
	
	/* 库存管理  */
	public function stock_manage(){
		$visitor_id = $_SESSION['user']['id']; 
	    $pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$shop=empty($_REQUEST['shop'])?'':trim($_REQUEST['shop']);
		$goodsinfo=empty($_REQUEST['goods'])?'':trim($_REQUEST['goods']);
		$starttime=empty($_REQUEST['starttime'])?'':trim($_REQUEST['starttime']);
		$endtime=empty($_REQUEST['endtime'])?'':trim($_REQUEST['endtime']);
		$name=empty($_REQUEST['name'])?'sl.id':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		$receiver_name=empty($_REQUEST['receiver_name'])?'':trim($_REQUEST['receiver_name']);
		$oid=empty($_REQUEST['oid'])?'':trim($_REQUEST['oid']);
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where= " visitor_id in($s_str)";
		}else{
			$where= " visitor_id = '$visitor_id'";
		}
		$shop_list=M('shop')->where($where)->field("id,shop_name")->select();
		$goods_list=M("goods")->where($where)->field("id,code,title")->order('id desc')->select();
		
		$goods=D('goods');
		
		$res=$goods->goods_stock_list($pageNo,$pageSize,'',$goodsinfo,$starttime,$endtime,$name,$asc,$receiver_name,$oid);

		$this->assign('stock_list',$res['stock_list']); 
		$this->assign('shop_list',$shop_list);
		$this->assign('goods_list',$goods_list);
		$this->assign('count',$res['count']);
		$this->assign('statistics',$res['statistics']);
		$this->assign('data',$res['data']);
		$this -> display();
	}
	
	/* 商品库存列表  */
	public function stock_list(){
		$visitor_id = $_SESSION['user']['id'];
	    $pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$shop=empty($_REQUEST['shop'])?'':trim($_REQUEST['shop']);
		$goods_id=empty($_REQUEST['goods'])?'':trim($_REQUEST['goods']);
		$starttime=empty($_REQUEST['starttime'])?date('Y-m-d 00:00:00'):trim($_REQUEST['starttime']);
		$endtime=empty($_REQUEST['endtime'])?'':trim($_REQUEST['endtime']);
		$name=empty($_REQUEST['name'])?'id':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		$receiver_name=empty($_REQUEST['receiver_name'])?'':trim($_REQUEST['receiver_name']);
		$oid=empty($_REQUEST['oid'])?'':trim($_REQUEST['oid']);
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where= " visitor_id in($s_str)";
		}else{
			$where = " visitor_id = '$visitor_id'";
		}
		
		$shop_list=M('shop')->where($where)->field("id,shop_name")->select();
		
		$goods_list=M('goods')->where($where)->field('id,title,short_name,code')->order('id desc')->select();
		
		$goods=D('goods');
		$res=$goods->total_stock_list('','',$goods_id,$starttime,$endtime,$name='id',$asc='desc');
		
		if(!empty($pageNo) && !empty($pageSize))
		{
			foreach($res['stock_list'] as $k=>$v)
			{
				if(($pageNo-1)*$pageSize <= $k && $k <$pageNo*$pageSize)
				{
					$stock_list[$k]=$v;
				}
			}
		}
		else
		{
			$stock_list=$res['stock_list'];
			
	    }
		
		$count['count']=count($res['stock_list']);
		$count['countpage']=ceil($count['count']/$pageSize);
		$count['pageSize']=$pageSize;
		$count['pageNo']=$pageNo;
		$count['num']=$count['countpage']-4;
		
		$this->assign('stock_list',$stock_list);
		$this->assign('shop_list',$shop_list);
		$this->assign('goods_list',$goods_list);
		$this->assign('count',$count);
		$this->assign('statistics',$res['statistics']);
		$this->assign('data',$res['data']);
		$this -> display();
	}
	
	/*  入库登记   */
	public function stock_info(){
		$visitor_id = $_SESSION['user']['id'];
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where= " visitor_id in($s_str)";
		}else{
			$where = " visitor_id = '$visitor_id'";
		}
		$goods=D('goods');
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		
		if(!empty($id))
		{   
			$stockinfo=$goods->get_goods_stock($id);
			$this->assign("stockinfo",$stockinfo);
		}
		 
	    $shop_list=M('shop')->where($where)->field("id,shop_name")->select();
		
		$goods_list=M('goods')->where($where)->field('id,code,title,short_name')->select();
		
		$this->assign("shop_list",$shop_list);
		$this->assign("goods_list",$goods_list);
		$this->assign("user_info",$user_info);
		$this->assign("jumpurl",$_SERVER['HTTP_REFERER']);
		$this->display();
		
	}
	
	/* 获取店铺对应的商品 */
	public function shop_goods(){
		$visitor_id = $_SESSION['user']['id'];    
		$shop=empty($_POST['shop'])?'':$_POST['shop'];
		
		$where='1';
		if(!empty($shop))
		{
			$where.=' and shop_id='.$shop;
		}
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where.= " and visitor_id = '$visitor_id'";
		}
		$goods_list=M('goods')->field('id,title,short_name,code')->where($where)->select();
		
		$str="<option value=''>请选择</option>";
		foreach($goods_list as $v)
		{   
		    if(empty($v['short_name'])) 
			{
				$str.="<option value='".$v['id']."'>".$v['code']."</option>";
			}
			else
			{
				$str.="<option value='".$v['id']."'>".$v['code']." (".$v['short_name'].")</option>"; 
			}
			
		}
		
		echo $str; 
	}
	
	public function stock_add(){
         
		$id=empty($_REQUEST['id'])?'':trim($_REQUEST['id']);
		$jumpurl=empty($_REQUEST['jumpurl'])?'':$_REQUEST['jumpurl'];
		
	    //$data['shop']=empty($_REQUEST['shop'])?'':trim($_REQUEST['shop']);
		$data['name']=empty($_REQUEST['name'])?'':trim($_REQUEST['name']); 
		$data['phone']=empty($_REQUEST['phone'])?'':trim($_REQUEST['phone']);
		$data['address']=empty($_REQUEST['address'])?'':trim($_REQUEST['address']);
		$data['goods_id']=empty($_REQUEST['goods'])?0:trim($_REQUEST['goods']);
		$data['num']=empty($_REQUEST['num'])?'':trim($_REQUEST['num']);
		$data['amount']=empty($_REQUEST['amount'])?'':trim($_REQUEST['amount']);
		$data['storage_time']=empty($_REQUEST['storage_time'])?'':trim($_REQUEST['storage_time']);
		$data['remarks']=empty($_REQUEST['remarks'])?'':trim($_REQUEST['remarks']);
		$data['modify_time']=date('Y-m-d H:i:s');
		$data['visitor_id']	 = $_SESSION['user']['id'];
		$visitor_id = $_SESSION['user']['id']; 
		$log=M('stock_log');
		
		$goodsinfo=D('goods')->goods_info_id($data['goods_id']);
		
		if(empty($id))
		{
			$data['add_time']=date('Y-m-d H:i:s');
			$res=$log->add($data);
			
			if($res)
			{
				$where_sto['id'] = $data['goods_id'];
				$where_sto['visitor_id'] = $_SESSION['user']['id'];
				$goods_stock['stock']=$goodsinfo['stock']+$data['num'];
				$goods_stock['visitor_id']	=	$_SESSION['user']['id'];
				$reg=M('goods')->where($where_sto)->save($goods_stock);   /* 更新商品库存 */
			}
			
			header('Location:stock_info');
		}
		else
		{   
		    $stockinfo=D('goods')->get_goods_stock($id);
			
			$res=$log->where("id='$id' and visitor_id = '$visitor_id'")->save($data);
			
			if($res!==false)
			{   
				if($stockinfo['num']!=$data['num'])
				{
					$where_sto['id'] = $data['goods_id'];
					$where_sto['visitor_id'] = $_SESSION['user']['id'];
					$goods_stock['stock']=$goodsinfo['stock']-$stockinfo['num']+$data['num'];
					$goods_stock['visitor_id']	=	$_SESSION['user']['id'];
				    $reg=M('goods')->where($where_sto)->save($goods_stock);   /* 更新商品库存 */
				}
				
			}
			
			header('Location:'.$jumpurl); 
		}
	}
	
	public function stock_delete()
	{
		$visitor_id = $_SESSION['user']['id'];  
		$id=empty($_REQUEST['id'])?0:$_REQUEST['id'];
		$stock=M('stock_log');
		$where = "id='$id' ";
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
		}
		$res=$stock->where($where)->delete();
		
		header("Location:".$_SERVER['HTTP_REFERER']);
	}
	
	/* 导出库存列表数据   */
	public function stock_export()
	{   
		$goods_id=empty($_REQUEST['goods'])?'':trim($_REQUEST['goods']);
		$starttime=empty($_REQUEST['starttime'])?date('Y-m-d 00:00:00'):trim($_REQUEST['starttime']);
		$endtime=empty($_REQUEST['endtime'])?'':trim($_REQUEST['endtime']);
		$name=empty($_REQUEST['name'])?'pay_time':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		
		$goods=D('goods');
		$res=$goods->total_stock_list('','',$goods_id,$starttime,$endtime,$name='id',$asc='desc');
		  
	    $arr['goods']='商品';
		$arr['stock_out']='出库';
		$arr['stock_in']='入库';
		$arr['stock']='当前库存';
		
		$endtime=empty($endtime)?date('Y-m-d H:i:s'):$endtime;
		
		$content="<table border='1' cellpadding='20px' style='border-volor:gray;vnd.ms-excel.numberformat:@'>";
        $content.= "<tr><td>时间</td><td colspan='3'>".$starttime . " - ".$endtime."</td></tr><tr></tr>\n";
		$content.= "<tr><td>".implode("</td><td>\t", $arr) . "</td></tr>\n";
		
		foreach($res['stock_list'] as $v)
		{   
		    $content .="<tr><td>";
			$data['goods']=$v['title'];
			$data['stock_out']=$v['stock_out'];
			$data['stock_in']=$v['stock_in'];
			$data['stock']=$v['stock'];
		
			$content .= implode("</td><td>"."\t", $data) . "</td></tr>\n";
		}
		$content.="</tr><tr></tr>";
		$content.="<tr><td>合计出库</td><td colspan='3'>".$res['statistics']['stock_out']."</td></tr>";
		$content.="<tr><td>合计入库</td><td colspan='3'>".$res['statistics']['stock_in']."</td></tr>";
		$content.="</table>";
		$filename="商品库存数据".date('YmdHis').".xls";
		header("Content-Disposition: attachment; filename=".$filename); 
		header("Content-Type: application/vnd.ms-excel");
		echo $content;
		
	} 
	
}
















